﻿package me.zhengjie.common.utils

import com.google.common.primitives.UnsignedLong
import java.util.*


class DeviceStringUtils {
    fun castNetModeToInteger(`val`: String): Int {
        return if (`val` == "DHCP") {
            1
        } else {
            0
        }
    }

    companion object {
        private val HEX_CHAR = charArrayOf(
            '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
        )

        /**
         * 方法一：
         * byte[] to hex string
         *
         * @param bytes
         * @return
         */
        fun bytesToHex1(bytes: ByteArray): String {
            // 一个byte为8位，可用两个十六进制位标识
            val buf = CharArray(bytes.size * 2)
            var a = 0
            var index = 0
            for (b in bytes) { // 使用除与取余进行转换
                a = if (b < 0) {
                    256 + b
                } else {
                    b.toInt()
                }
                buf[index++] = HEX_CHAR[a / 16]
                buf[index++] = HEX_CHAR[a % 16]
            }
            return String(buf)
        }

        /**
         * 方法二：
         * byte[] to hex string
         *
         * @param bytes
         * @return
         */
        fun bytesToHex2(bytes: ByteArray): String {
            val buf = CharArray(bytes.size * 2)
            var index = 0
            for (b in bytes) { // 利用位运算进行转换，可以看作方法一的变种
                buf[index++] = HEX_CHAR[b.toInt() ushr 4 and 0xf]
                buf[index++] = HEX_CHAR[b.toInt() and 0xf]
            }
            return String(buf)
        }

        /**
         * 方法三：
         * byte[] to hex string
         *
         * @param bytes
         * @return
         */
        fun bytesToHex3(bytes: ByteArray): String {
            val buf = StringBuilder(bytes.size * 2)
            for (b in bytes) { // 使用String的format方法进行转换
                buf.append(String.format("%02x", b.toInt() and 0xff))
            }
            return buf.toString()
        }

        /**
         * 将16进制字符串转换为byte[]
         *
         * @param str
         * @return
         */
        fun toBytes(str: String?): ByteArray {
            if (str == null || str.trim { it <= ' ' } == "") {
                return ByteArray(0)
            }
            val bytes = ByteArray(str.length / 2)
            for (i in 0 until str.length / 2) {
                val subStr = str.substring(i * 2, i * 2 + 2)
                bytes[i] = subStr.toInt(16).toByte()
            }
            return bytes
        }

        fun padRight(src: String, len: Int, ch: Char): String {
            val diff = len - src.length
            if (diff <= 0) {
                return src
            }
            val charr = CharArray(len)
            System.arraycopy(src.toCharArray(), 0, charr, 0, src.length)
            for (i in src.length until len) {
                charr[i] = ch
            }
            return String(charr)
        }

        fun padLeft(src: String, len: Int, ch: Char): String {
            val diff = len - src.length
            if (diff <= 0) {
                return src
            }
            val charr = CharArray(len)
            System.arraycopy(src.toCharArray(), 0, charr, diff, src.length)
            for (i in 0 until diff) {
                charr[i] = ch
            }
            return String(charr)
        }

        // Guava 中有 UnsignedLong 实现
        fun toUint64String(longValue: Long): String {
            val binaryString = java.lang.Long.toBinaryString(longValue)
            val unsignedLong = UnsignedLong.valueOf(binaryString, 2)
            return unsignedLong.toString()
        }

        fun castNetModeToString(`val`: Int): String {
            return if (`val` == 0) {
                "DHCP"
            } else {
                "STATIC"
            }
        }

        fun castBool2Int(`val`: Boolean): Int {
            return if (`val`) {
                1
            } else {
                0
            }
        }

        fun castInt2Bool(`val`: Int): Boolean {
            return if (`val` == 0) {
                false
            } else {
                true
            }
        }

        //testConvert
        /*@Throws(Exception::class)
        @JvmStatic
        fun main(args: Array<String>) {
            val bytes = byteArrayOf(
                0x77.toByte(),
                0xa6.toByte(),
                0x89.toByte(),
                0x1e.toByte(),
                0xec.toByte(),
                0x7e.toByte(),
                0x3f.toByte(),
                0xb6.toByte(),
                0x94.toByte(),
                0xec.toByte(),
                0xc8.toByte(),
                0xed.toByte(),
                0xe6.toByte(),
                0xf3.toByte(),
                0x5d.toByte(),
                0xe8.toByte(),
                0x7f.toByte(),
                0xbe.toByte(),
                0x50.toByte(),
                0x65.toByte(),
                0x56.toByte(),
                0xf1.toByte(),
                0x2e.toByte(),
                0x4b.toByte(),
                0x9c.toByte(),
                0xca.toByte(),
                0x2d.toByte(),
                0xd2.toByte(),
                0x5c.toByte(),
                0x89.toByte(),
                0x09.toByte(),
                0x25.toByte()
            )
            println("字节数组为：" + Arrays.toString(bytes))
            println("方法一：" + bytesToHex1(bytes))
            println("方法二：" + bytesToHex2(bytes))
            println("方法三：" + bytesToHex3(bytes))
            println("==================================")
            val str = "7E030161646D696E202020202020202020202020202020313233343536202020" +
                    "200100800102FF0D0700000000000000000000001706000000000000000000000000000000000000" +
                    "0000000F050000000000000000000000000004100500041B0400040A0500041A0400040905000419040" +
                    "0040805000418040004070500041704000406050004160400040505000415040004040500041404000403" +
                    "050004130400040205000412040004010500041104000400050004100400CBF37E"

            //String str = "77a6891eec7e3fb694ecc8ede6f35de87fbe506556f12e4b9cca2dd25c890925";
            println("转换后的字节数组：" + Arrays.toString(toBytes(str)))
            println(String(toBytes(str), Charsets.UTF_8))
            println("*" + padLeft("fuck", 12, '0') + "*")
            println("*" + padRight("fuck", 12, '0') + "*")
        }*/
    }
}
